package logger

import (
	"io"
	"log"
	"os"
	"sync"
	"sync/atomic"
)

var atomicLogger = atomic.Value{}
var lock *sync.Mutex = &sync.Mutex{}

func GetLogger() *log.Logger {
	if atomicLogger.Load() == nil {
		lock.Lock()
		defer lock.Unlock()
		if atomicLogger.Load() == nil {
			logFile, err := os.OpenFile("app.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
			if err != nil {
				log.Fatal("无法打开日志文件:", err)
			}
			_logger := log.New(io.MultiWriter(os.Stdout, logFile), "", log.LstdFlags|log.Lshortfile)
			atomicLogger.Store(_logger)
		}
	}
	return atomicLogger.Load().(*log.Logger)
}
